<template>
  <div>
    <el-button
      icon="el-icon-mobile"
      type="primary"
      plain
      size="medium"
      @click="onSave()"
      :disabled="!zIsOpenData"
      >保存菜单权限</el-button
    >
    <el-button
      icon="el-icon-refresh"
      type="primary"
      plain
      size="medium"
      @click="openData(true)"
      >刷新菜单权限</el-button
    >
    <el-button
      icon="el-icon-setting"
      type="primary"
      plain
      size="medium"
      @click="initSystemMenuList()"
      >初始化菜单</el-button
    >
    <div style="height:15px"></div>
    <el-tree
      :data="FRoleMenuList"
      show-checkbox
      node-key="menuIndexName"
      ref="refMenuTree"
      highlight-current
      :props="FTreeProps"
      accordion
    >
    </el-tree>
  </div>
</template>
<script  lang="ts">
import { Options } from "vue-class-component";
import TFormBase from "@/views/BaseForm";
import { IMenuItem } from "@/store";
import { TBaseRole, TMenuItem } from "@/baselib/right/BaseRightClass";
import elmHelper from "@/views/model/elmHelper";

//配合着用
@Options({
  name: "TFormRoleRightMenu",
  components: {},
  computed: {},
  data() {},
})
export default class TFormRoleRightMenu extends TFormBase {
  declare $refs: {
    refMenuTree: any;
  };
  private FIndeRole: TBaseRole = new TBaseRole();
  private FTreeProps = { label: "menuTitle", children: "menuChildren" };
  private FRoleMenuList: TMenuItem[] = [];
  public created() {
    this.FIndeRole = this.zContextParams;

    if (!this.zIsOpenData) {
      this.openData();
    }
  }
  private async openData(qHint: boolean = false) {
    this.zIsOpenData = false;
    var lResult = await this.$myBaseRightAPI.getRoleMenuList(
      this.FIndeRole.FRoleID
    );
    if (!lResult.resultSuccess) {
      this.$myMsgHelp.msgDialog(lResult.resultMsg);
      return;
    }
    //
    this.FRoleMenuList = lResult.resultData;
    //启用,禁用
    let lChecks = elmHelper.menuListGetCheckKeys(this.FRoleMenuList);
    this.$refs.refMenuTree.setCheckedKeys(lChecks);
    this.zIsOpenData = true;
    if (qHint) {
      this.$myMsgHelp.msgHintAutoClose(
        "刷新菜单成功",
        this.$myMsgHelp.msgSuccess
      );
    }
  }
  private async initSystemMenuList() {
    var lResult = await this.$myBaseRightAPI.initSystemMenuList();
    if (!lResult.resultSuccess) {
      this.$myMsgHelp.msgDialog(lResult.resultMsg);
      return;
    } else {
      this.$myMsgHelp.msgHintAutoClose(
        "初始化菜单至后台成功",
        this.$myMsgHelp.msgSuccess
      );
      this.openData();
    }
  }
  private async onSave() {
    let lChecks = this.$refs.refMenuTree.getCheckedKeys();
    if (lChecks == null || lChecks.length == 0) {
      let lConfirm = await this.$myMsgHelp.msgDialogConfirm(
        "确定要清除此角色所有菜单"
      );
      if (!lConfirm) {
        return;
      }
    }
    let lResult = await this.$myBaseRightAPI.saveRoleMenu(
      this.FIndeRole.FRoleID,
      lChecks
    );
    if (!lResult.resultSuccess) {
      this.$myMsgHelp.msgDialog(lResult.resultMsg);
      return;
    } else {
      this.$myMsgHelp.msgHintAutoClose(
        "保存角色菜单权限成功",
        this.$myMsgHelp.msgSuccess
      );
    }
  }
}
</script>